<template>
  <div class="dashboard-container">
    <common-view
      :table-prop="tableProp"
      :form-rules="formRules"
      module-name="classRoomList"
      :search="getClassRoomList"
      :add="addClassRoom"
      :delete="deleteClassRoom"
      :update="updateClassRoom"
      :get-form-data="getFormData"
      :get-search-data="getSearchData"
    />

  </div>
</template>

<script>

import CommonView from '@/components/commonView/index'
import { getClassRoomList, addClassRoom, deleteClassRoom, updateClassRoom } from '@/api/classroom'
import { getProfessionList } from '@/api/profession'
import { getUserList } from '@/api/user'

export default {
  name: 'ClassRoomList',
  components: { 'common-view': CommonView },
  data() {
    return {
      getClassRoomList,
      addClassRoom,
      deleteClassRoom,
      updateClassRoom,
      userList: [],
      professionList: [],
      tableData: [],
      tableProp: [
        {
          prop: 'classYear',
          label: '年级'
        },
        {
          prop: 'professionId',
          label: '专业'
          // formatter: value => {
          //   const findProfession = this.professionList.find(profession => profession.id === value.professionId)
          //   return findProfession !== undefined ? findProfession.professionName : '-'
          // }
        },
        {
          prop: 'classCode',
          label: '班级'
        }, {
          prop: 'classTeacherId',
          label: '班主任'
          // formatter: value => {
          //   const findUser = this.userList.find(user => user.id === value.classTeacherId)
          //   return findUser !== undefined ? findUser.nickName : '-'
          // }
        }, {
          prop: 'createTime',
          label: '创建时间'
        }, {
          prop: 'updateTime',
          label: '修改时间'
        }
      ],
      formRules: {
        classCode: [
          { required: true, message: '请输入班级编号', trigger: 'change' },
          { type: 'number', message: '班级编号只能为数字', trigger: 'change' }
        ],
        classYear: [
          { required: true, message: '请选择年级', trigger: 'change' }
        ],
        professionId: [
          { required: true, message: '请选择专业', trigger: 'change' }
        ],
        classTeacherId: [
          { required: true, message: '请选择班主任', trigger: 'change' }
        ]
      }
    }
  },
  mounted() {
    // 数据初始化
    this.init()
  },
  methods: {
    async init() {
      const [professionList, userList] = await Promise.all([getProfessionList({}), getUserList({})])
      this.userList = userList.list
      this.professionList = professionList.list
    },
    // 新增、编辑表单转换
    getFormData(formData, handle, id) {
      console.log('转换前', formData)
      const { professionId, classTeacherId, classYear, classCode } = formData
      const newFormData = { professionId, classTeacherId, classYear, classCode }
      if (handle === 'edit') {
        newFormData['classYear'] = newFormData['classYear'] + ''
        newFormData['id'] = id
      }
      return newFormData
    },
    // 查询条件转换
    getSearchData(formData) {
      const { classYear, professionName } = formData
      return {
        classYear, professionName
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.dashboard {
  &-container {
    margin: 30px;
  }
}
</style>
